/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package dw2;

import java.awt.*;
import java.util.Random;
import java.util.ArrayList;
import java.util.Arrays;
/**
 * 
 * @author Brian
 */
public class npcAlienShip extends NPC {
    protected int life_value = 0;
    protected int cycle_count = 0;
    protected int health = 10;
    protected int state = 0;
    protected int death_delay = 0;
    protected static Random randomGenerator = new Random();
    protected static ArrayList<Image> image = new ArrayList<Image>();

    public static void initImage(Image images[])
    {
        image.addAll(Arrays.asList(images));
        
    }

     public npcAlienShip(int x, int y, int direction_to_face,int points){
        super(x-(150),y-image.get(0).getHeight(null)-(150),direction_to_face,points);
        WALK_SPEED = 2;
        ATTACK_POWER = 5;
        FIRING_RATE = 250;
        super.x += randomGenerator.nextInt(50);
        super.y += randomGenerator.nextInt(50);
        graphics.add(new DWGraphics_Animation(image.get(0),10,4));  //normal movement
        graphics.add(new DWGraphics_Animation(image.get(1),1,5));   //explosion --not used
        graphics.add(new DWGraphics_Animation(image.get(2),1,8));   //hurt
    }

     public boolean fire_gun(Main mainparent,Point head_pos){
        bulletY = y + 10;
        bulletX = x;
        if(head_pos.y < mainparent.getHeight()/2)
        {
            fireCount++;

            if(fireCount == FIRING_RATE)
            {
                new AePlayWave("Sounds\\sndGun.wav").start();
                Bullet bullet_to_fire = new Bullet(mainparent,x, y, Color.blue, bullet_size,ATTACK_POWER);
                double bullet_direction = Worm.pointDirection(x, y, head_pos.x, head_pos.y);
                double speed = ATTACK_BULLET_SPEED;
                bullet_to_fire.setXYspeed(Worm.getXspeed(bullet_direction,speed), Worm.getYspeed(bullet_direction,speed));
                mainparent.addBullet(bullet_to_fire);
                fireCount = 0;
                return true;
            }
        }
        if(head_pos.y < mainparent.getHeight()/2)
        {
            fireCount++;
            if(fireCount == FIRING_RATE)
            {
                new AePlayWave("Sounds\\sndGun.wav").start();
                Bullet bullet_to_fire = new Bullet(mainparent,x,y, Color.yellow, bullet_size,ATTACK_POWER);
                double bullet_direction = Worm.pointDirection(x, y, head_pos.x, head_pos.y);
                double speed = ATTACK_BULLET_SPEED;
                bullet_to_fire.setXYspeed(Worm.getXspeed(bullet_direction,speed), Worm.getYspeed(bullet_direction,speed));
                mainparent.addBullet(bullet_to_fire);
                fireCount = 0;
                return true;
            }
        }
        return false;
    }


    public void action(Main mainparent,Worm player)
    {
         fire_gun(mainparent,player.getHeadPosition());

         if(state == 0){
            graphics_index = 0;
         } if(state == 2){
            Hurt();
            cycle_count++;
        }

        cycle_count++;
        this.player = player;
        move(mainparent,player.getHeadPosition());


        if(player.hitHead(graphics.get(graphics_index).getBounds())==true)
        {
            health--;
            state = 2;
            cycle_count = 0;
            if(health == 0){
                new AePlayWave("Sounds\\bit1.wav").start();
                 deathParticles(mainparent,player);  
                 mainparent.removeNPC(this);
                 player.add_points(points);
                 return;
            }
        }
        if(cycle_count > 50){
            state = 0;
            cycle_count = 0;
        }
    }
    
    public void BlowUp(){
        graphics_index = 1;
    }
    public void Hurt(){
        graphics_index = 2;
    }
     public void deathParticles(Main mainparent,Worm player)
    {
        int center_x = x + graphics.get(graphics_index).getWidth()/2;
        int center_y = y + graphics.get(graphics_index).getHeight();
        for(int w = 0;w<death_particle_count;w++)
        {
            mainparent.addParticle(new Particle(center_x,center_y,Color.gray,2,5));
        }

    }
}
